

/**
 * [ajaxHttp ajax请求方法]
 * @Author   王文凡
 * @DateTime 2017-10-13
 * @version  1.0
 * @param    {[type]}   param [description]
 * @return   {[type]}         [description]
 */
function ajaxHttp(param) {
    var settings = $.extend({
        type: 'get',
        async: true,
        cache: 'false',
        dataType: 'json'
    }, param);
    if (settings.btn) {
        if (settings.btn.hasClass('disabled')) {
            return false;
        }
        settings.btn.addClass('disabled');
    }
    $.ajax({
        type: settings.type,
        cache: settings.cache,
        async: settings.async,
        dataType: settings.dataType,
        data: settings.data,
        url: settings.url,
        success: function(data) {
            if (data.status == 0) {
                if ($.isFunction(settings.success)) {
                    settings.success(data);
                } else {
                    $.pjax.reload('#pjax-container');
                    toastr.success(data.message);
                }

            } else {
                if ($.isFunction(settings.fail)) {
                    settings.fail(data);
                } else {
                    toastr.error(data.message);
                }
            }
        },
        complete: function(XMLHttpRequest, textStatus) {

            var ret_code = XMLHttpRequest.status;
            var ret_json = XMLHttpRequest.responseJSON;
            var message = {};
            for (var i in ret_json) {
                message[i] = ret_json[i][0];
            }
            if (ret_code == 422) {
                for (var p in message) {
                    toastr.error(message[p]);
                    break;
                }
            } else if (textStatus == 'error') {
                toastr.error(ret_json.message ? ret_json.message : '请求失败');
            }
        },
        error: function(data) {
            //toastr.error(data.Msg);
        }
    });
}

/**
 * 批量修改状态操作
 */
function batch(state, ids, url) {
    if (ids == '') return false;
    var param = {
        type: 'post',
        url: url,
        data: {
            _token: LA.token,
            ids: ids,
            state: state
        },
        success: function(data) {
            $.pjax.reload('#pjax-container');
            toastr.success(data.message);
        }
    };
    ajaxHttp(param);
}

//ajax get请求
$('.ajax-get').click(function() {
    var target;
    var that = this;
    if ($(this).hasClass('confirm')) {
        if (!confirm('确认要执行该操作吗?')) {
            return false;
        }
    }
    if ((target = $(this).attr('href')) || (target = $(this).attr('url'))) {
        $.getJSON(target).success(function(data) {
            //成功后有链接提示+跳转，否则提示+刷新
            if (data.status == 0) {
                swal(data.message, '', 'success');
                setTimeout(function() {
                    if (data.url && data.url != "javascript:history.back(-1);") {
                        location.href = data.url;
                    } else {
                        location.reload();
                    }
                }, 1500);
            } else {
                //弹出提示
                swal(data.message, '', 'error');
            }

        });

    }
    return false;
});

/**
 * [findStatus 服务器状态查询]
 * @Author   王文凡
 * @DateTime 2018-03-10
 * @version  1.0
 * @param    {[type]}   url [description]
 * @return   {[type]}       [description]
 */
function findStatus(url) {
    var sid = $("#server_id").val();
    if (sid == '') {
        swal('请填写服务器id', '', 'error');
        return;
    }
    var param = {
        data: {
            server_id: sid
        },
        url: url,
        success: function(data) {
            if (data.status) {
                swal(data.message, '', 'error');
            } else {
                swal(data.message);
            }
        }
    };
    ajaxHttp(param);
}

/**
 * [changeSubmit 修改服务器状态]
 * @Author   王文凡
 * @DateTime 2018-03-10
 * @version  1.0
 * @param    {[type]}   url [description]
 * @return   {[type]}       [description]
 */
function changeSubmit(url) {
    var sid = $("#server_id").val();
    var state = $("#stateVal").val();
    if (sid == '') {
        swal('请填写服务器id', '', 'error');
        return;
    }
    if (state == '') {
        swal('请选择状态', '', 'error');
        return;
    }
    var param = {
        type: 'post',
        data: {
            _token: LA.token,
            id: sid,
            state: state
        },
        url: url
    };
    ajaxHttp(param);
}

//获取get参数
function getPar(par) {
    //获取当前URL
    var local_url = document.location.href;
    //获取要取得的get参数位置
    var get = local_url.indexOf(par + "=");
    if (get == -1) {
        return '';
    }
    //截取字符串
    var get_par = local_url.slice(par.length + get + 1);
    //判断截取后的字符串是否还有其他get参数
    var nextPar = get_par.indexOf("&");
    if (nextPar != -1) {
        get_par = get_par.slice(0, nextPar);
    }
    return get_par;
}


function checkAll(class_name) {
    var selected = [];
    $(class_name + ':checked').each(function() {
        selected.push($(this).data('id'));
    });
    return selected;
}

function playerView(self){
    var name = $(self).attr('data-name');
    var id = $(self).attr('data-id');
    $("#player_id").val(id);
    $("#gameuser-modal").modal();
    $("#player_name").html(name);
}

function playerFun(url){
    var serverid = getPar('serverid');
    var sid = serverid ? serverid : 1;
    var id = $("#player_id").val();
    var new_url = url+"&id="+id+"&serverid="+sid;
    window.open(new_url);
}